// pages/ticket/ticket.js
Page({
  data: {
    tickets: [
      { id: 1, name: '象鼻山', image: '/images/banner1.png', openingHours: '7:30-18:00', suggestedDuration: '2-3小时', price: 75, originalPrice: 90 },
      { id: 2, name: '叠彩山', image: '/images/banner1.png', openingHours: '8:00-17:30', suggestedDuration: '1-2小时', price: 45, originalPrice: 60 },
      { id: 3, name: '漓江', image: '/images/banner1.png', openingHours: '全天', suggestedDuration: '3-4小时', price: 200 },
      { id: 4, name: '阳朔', image: '/images/banner1.png', openingHours: '全天', suggestedDuration: '半天至一天', price: 150 },
      { id: 5, name: '龙脊梯田', image: '/images/banner1.png', openingHours: '8:00-17:00', suggestedDuration: '半天', price: 80 },
    ],
    searchQuery: '',
    filteredTickets: []
  },

  onLoad: function() {
    this.setData({ filteredTickets: this.data.tickets });
  },

  onReady: function() {
    // 页面初次渲染完成
  },

  onShow: function() {
    // 页面显示
  },

  onHide: function() {
    // 页面隐藏
  },

  onUnload: function() {
    // 页面卸载
  },

  // 搜索输入框值改变时触发
  onSearchInput(e) {
    this.setData({ searchQuery: e.detail.value });
  },

  // 点击搜索按钮时触发
  onSearch() {
    const query = this.data.searchQuery.toLowerCase();
    const filteredTickets = this.data.tickets.filter(ticket =>
      ticket.name.toLowerCase().includes(query)
    );
    this.setData({ filteredTickets });
  },

  // 购买门票
  buyTicket(e) {
    const ticketId = e.currentTarget.dataset.id;
    const selectedTicket = this.data.tickets.find(ticket => ticket.id === ticketId);
    console.log('购买:', selectedTicket);

    // 在这里可以添加跳转到支付页面或者直接调用支付API的逻辑
    wx.showToast({
      title: `正在购买 ${selectedTicket.name}`,
      icon: 'loading',
      duration: 2000
    });

    setTimeout(() => {
      wx.showToast({
        title: '购买成功',
        icon: 'success',
        duration: 2000
      });
    }, 2000);
  }
});